package com.jdew.thkj.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.adapter.UpgradeAdapter;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.bean.RisingRecordBean;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.manager.WrapContentLinearLayoutManager;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class UpgradeRecordActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.recyclerview)
    RecyclerView recyclerView;
    @Bind(R.id.smartrefresh)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.tablayout)
    TabLayout tabLayout;
    @Bind(R.id.noresult)
    TextView noresult;
    @Bind(R.id.result_view)
    RelativeLayout resultView;
    @Bind(R.id.search)
    EditText search;
    @Bind(R.id.clear)
    ImageView clear;
    @Bind(R.id.tosearch)
    TextView tosearch;
    private UpgradeAdapter adapter;
    private List<RisingRecordBean.DataBean> list;
    private List<RisingRecordBean.DataBean> list2;
    private int level = 2;
    private int page = 1;

    @Override
    public int bindLayout() {
        return R.layout.activity_upgraderecord;
    }

    @Override
    public void initView(View view) {
        back.setVisibility(View.VISIBLE);
        title.setText("升级记录");
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        back.setOnClickListener(this);
        list = new ArrayList<>();
        list2 = new ArrayList<>();
        adapter = new UpgradeAdapter(this, list);
        tabLayout.addTab(tabLayout.newTab().setText("高级惠员"));
        tabLayout.addTab(tabLayout.newTab().setText("超级教练"));
        tosearch.setOnClickListener(this);
        clear.setOnClickListener(this);
        getRecord();
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() == 0) {
                    level = 2;
                } else {
                    level = 0;
                }
                page = 1;
                refreshLayout.setEnableLoadMore(true);
                getRecord();
                refreshLayout.finishRefresh();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerView.setAdapter(adapter);
        refreshLayout.setRefreshHeader(new MaterialHeader(this));
        refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                getRecord();
                refreshLayout.finishLoadMore();
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                refreshLayout.setEnableLoadMore(true);
                getRecord();
                refreshLayout.finishRefresh();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            case R.id.tosearch: {
               getRecord();
            }
            break;
            case R.id.clear: {
               search.setText("");
            }
            break;
            default:
                break;
        }
    }

    public void getRecord() {
        HashMap<String, Object> map = new HashMap();
        map.put("pageIndex", page);
        map.put("userLevel", level);
        map.put("UserPhone", sp.getString("userPhone", ""));
        map.put("upNameOrPhone",search.getText().toString().trim());
        String timesamp= RegexValidateUtils.getTime13();
        String numsamp=RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).RisingRecord(map,RegexValidateUtils.getUniquePsuedoID(), timesamp, numsamp, MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp + numsamp + RegexValidateUtils.getUniquePsuedoID() + sp.getString("SignToken", "") + "pageIndex" + page+"userLevel" + level+"UserPhone" + sp.getString("userPhone", "")+"upNameOrPhone" + search.getText().toString().trim())).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<RisingRecordBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(RisingRecordBean bean) {
                if (bean.getNum() == 0) {
                    recyclerView.setVisibility(View.GONE);
                    resultView.setVisibility(View.VISIBLE);
                    noresult.setText("您还没有升级记录哦");
                    return;
                }
                if (page == 1) {
                    list.clear();
                }
                recyclerView.setVisibility(View.VISIBLE);
                resultView.setVisibility(View.GONE);
                list.addAll(bean.getData());
                adapter.setDataList(list);
                if (bean.getData().size() < 20 || bean.getNum() % 20 == 0) {
                    refreshLayout.setEnableLoadMore(false);
                    return;
                }
            }
        });
    }
}
